//
//  ZCOrderChoiseCouponVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/4/23.
//  Copyright © 2019 jeking. All rights reserved.
//

#import "ZCOrderChoiseCouponVC.h"
#import "ZCOrderChoiseCouponCell.h"

@interface ZCOrderChoiseCouponVC ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSDictionary *selectDict;
@end

@implementation ZCOrderChoiseCouponVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.navTitle = @"优惠券";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCOrderChoiseCouponCell" bundle:nil]
         forCellReuseIdentifier:@"ZCOrderChoiseCouponCell"];
    [self setRightItemWithTitle:@"完成" action:@selector(rightItemClickAction)];
    if (self.selectId != nil && self.selectId.length != 0) {
        for (NSDictionary *dict in self.list) {
            if ([[dict valueForKey:@"id"] isEqualToString:self.selectId]) {
                NSInteger row = [self.list indexOfObject:dict];
                NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
                [self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
            }
        }
    }
    
    [self setTableView:self.tableView noDataWithTitle:@"暂无数据" des:nil andImageName:nil];
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSDictionary *dict = self.list[row];
    ZCOrderChoiseCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCOrderChoiseCouponCell"
                                                                    forIndexPath:indexPath];
    cell.dict = [dict valueForKey:@"coupon"];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 90.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 113.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    if (self.selectDict == self.list[row]) {
        self.selectDict = nil;
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    } else {
        self.selectDict = self.list[row];
    }
}


- (void)rightItemClickAction {
    if (self.btnClickCallback) {
        self.btnClickCallback(self.selectDict);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setList:(NSArray *)list {
    if (list == nil || list.count == 0) {
    } else {
        _list = list;
        [self.tableView reloadData];
    }
}

@end
